package com.jdbc.student;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * JDBCUtil.java JDBC工具类，获取连接，关闭资源
 * @author Enginner-Jie
 */
public class JDBCUtil {
	/**
	 * 连接数据库
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception{
		Properties pros = new Properties();
		FileInputStream fis = new FileInputStream(new File("jdbc.properties"));
		pros.load(fis);
		
		String url = pros.getProperty("url");
		String user = pros.getProperty("user");
		String password = pros.getProperty("password");
		String driverClass = pros.getProperty("driver");
		fis.close();
		
		Class.forName(driverClass);
		Connection	connection = DriverManager.getConnection(url, user, password);
	//	System.out.println("Connected to Database!");
		return connection;
	}
	/**
	 * 关闭资源
	 * @param rSet
	 * @param statement
	 * @param connection
	 */
	public static void release(ResultSet rSet,Statement statement,Connection connection) {
		if (rSet != null) {
			try {
				rSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
